﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atom.Entity;
using Atom.DAL;
using Atom.Utility;
using System.Data;

namespace Atom.BLL
{
    public class C3_2BLL : BaseBLL
    {
        public List<C3_2Entity> GetC3_2Entity(DateTime dtBegin, DateTime dtEnd, string whereFilter)
        {
            List<C3_2Entity> listC3_2 = new List<C3_2Entity>();
            try
            {
                LeaveHosRecordDAL dal = new LeaveHosRecordDAL();


                int temp = 0;

                List<BZEntity> bzEntitys = ReadXml.GetBZEntity();
                List<CodeDetailEntity> codedetails = ReadXml.GetCodeDetailEntity();//得到药品监测项目
                List<ItemDetailEntity> itemdetails = ReadXml.GetItemDetailEntity();//得到检查监测项目

                //循环病种
                foreach (BZEntity bzentity in bzEntitys)
                {
                    DataSet dsRecord = dal.GetC3_2RecordBaseInfo(dtBegin, dtEnd, Constants.MEDICARENO, bzentity.BZName);


                    for (int i = 0; i < Constants.DIAGNOSE_COUNT; i++)
                    {
                        if (dsRecord != null && dsRecord.Tables.Count > 0)
                        {

                            if (dsRecord.Tables[0].Rows.Count > i)
                            {

                                DataRow row = dsRecord.Tables[0].Rows[i];
                                C3_2Entity entity = new C3_2Entity();
                                entity.BZType = bzentity.Index;
                                entity.BZName = bzentity.BZName;

                                //基本信息
                                entity.HYZID = row["MEDICARESAFETYID"].ToString();
                                entity.DiagnoseID = row["CASEID"].ToString();//MEDICARENO
                                entity.PatientName = row["PATIENTNAME"].ToString();
                                entity.Sex = row["SEX"].ToString();
                                entity.Age = Convert.ToInt32(row["AGE"]);
                                entity.TotalFee = Convert.ToDecimal(row["TOTALCONSUMEMONEY"]);
                                entity.ZFFee = Convert.ToDecimal(row["PERSONALMONEY"]);
                                entity.BedCount = Convert.ToInt32(row["INHOSDAYS"]);

                                #region 保留
                              
                                //#region 得要药品的详细信息

                                ////药品使用情况(抗生素)
                                //DataSet dsTemp = new DataSet();
                                //dsTemp = dal.GetC3_2RecordYPInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), codedetails[Constants.CODEDETAIL_KSS].Name);

                                //int kssls = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count > 1) ? 0 : dsTemp.Tables[0].Rows.Count;
                                //switch (kssls)
                                //{
                                //    case 0:
                                //        entity.KSSLS = "D";
                                //        entity.KSSName = "";
                                //        break;
                                //    case 1:
                                //        entity.KSSLS = "A";
                                //        entity.KSSName = dsTemp.Tables[0].Rows[0]["TRADENAME"].ToString();
                                //        entity.KSSYPFee = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"].ToString());

                                //        break;
                                //    case 2:
                                //        entity.KSSLS = "B";
                                //        entity.KSSName = dsTemp.Tables[0].Rows[0]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[1]["TRADENAME"].ToString();
                                //        entity.KSSYPFee = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[1]["MONEY"]);

                                //        break;
                                //    case 3:
                                //        entity.KSSLS = "C";
                                //        entity.KSSName = dsTemp.Tables[0].Rows[0]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[1]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[2]["TRADENAME"].ToString();
                                //        entity.KSSYPFee = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[1]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[2]["MONEY"]);

                                //        break;
                                //    default:
                                //        break;
                                //}

                                //// entity.KSSLS = switch kssls ;
                                //// entity.KSSName = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count > 1) ? "" : dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"].ToString();

                                ////药品使用情况(激素类)
                                //dsTemp = dal.GetC3_2RecordYPInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), codedetails[Constants.CODEDETAIL_JS].Name);

                                //entity.JSCount = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count > 1) ? "" : dsTemp.Tables[0].Rows.Count.ToString();
                                //if (dsTemp != null && dsTemp.Tables[0].Rows.Count > 0)
                                //{
                                //    foreach (DataRow item in dsTemp.Tables[0].Rows)
                                //    {
                                //        entity.JSName += item["TRADENAME"].ToString() + "/";
                                //        entity.JSYPFee += Convert.ToDecimal(item["MONEY"]);

                                //    }
                                //    entity.JSName = entity.JSName.Remove(entity.JSName.Length - 2);
                                //}
                                //else
                                //{
                                //    entity.JSName = "--";
                                //    entity.JSYPFee = 0;
                                //}



                                ////药品使用情况(营养类)
                                //dsTemp = dal.GetC3_2RecordYPInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), codedetails[Constants.CODEDETAIL_YY].Name);

                                //entity.YYCount = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count > 1) ? "" : dsTemp.Tables[0].Rows.Count.ToString();
                                //if (dsTemp != null && dsTemp.Tables[0].Rows.Count > 0)
                                //{
                                //    foreach (DataRow item in dsTemp.Tables[0].Rows)
                                //    {
                                //        entity.YYName += item["TRADENAME"].ToString() + "/";
                                //        entity.YYYPFee += Convert.ToDecimal(item["MONEY"]);
                                //    }
                                //    entity.YYName = entity.YYName.Remove(entity.YYName.Length - 2);
                                //}
                                //else
                                //{
                                //    entity.YYName = "--";
                                //    entity.YYYPFee = 0;
                                //}




                                //entity.YPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"]); ;
                                ////entity.KSSYPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count > 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"]); ;
                                ////entity.JSYPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count > 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"]); ;
                                ////entity.YYYPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count > 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"]); ;
                                //entity.OtherYPFee = 0;

                                //#endregion

                                //#region 辅助检查费用

                                ////辅助检查费用
                                ////entity.OtherFZJCFee = 0;
                                ////entity.YLSBJCFee = 0;
                                ////entity.YLSBJCCount = 0;

                                //entity.YLSBJCYXCount = 0;
                                ////entity.YLSBCountMaxName = "";
                                ////entity.YLSBCountMax = 0;
                                //entity.YLSBJCMaxYXCount = 0;
                                ////entity.YLSBJCMaxFee = 0;


                                //dsTemp = dal.GetC3_2RecordZLBaseInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), Constants.CHARGESUMMARY_ZL);

                                //entity.OtherFZJCFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]);


                                //dsTemp = dal.GetC3_2RecordZLInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), itemdetails[0].Name);

                                ////entity.JSCount = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count > 1) ? "" : dsTemp.Tables[0].Rows.Count.ToString();
                                //string chargeItemname = "";
                                //int chargeItemMax = 0;
                                //decimal chargeItemFee = 0;
                                //if (dsTemp != null && dsTemp.Tables[0].Rows.Count > 0)
                                //{
                                //    foreach (DataRow item in dsTemp.Tables[0].Rows)
                                //    {
                                //        int count = Convert.ToInt32(item["CHARGECOUNT"]);
                                //        if (count > chargeItemMax)
                                //        {
                                //            chargeItemMax = count;
                                //            chargeItemname = item["CHARGEITEM"].ToString();
                                //            chargeItemFee = Convert.ToDecimal(item["MONEY"]);
                                //        }
                                //        entity.YLSBJCFee += Convert.ToDecimal(item["MONEY"]);
                                //        entity.YLSBJCCount += Convert.ToInt32(item["CHARGECOUNT"]);


                                //    }

                                //}
                                //else
                                //{
                                //    entity.YLSBJCFee = 0;
                                //    entity.YLSBJCCount = 0;
                                //}
                                ////设置使用次数最大的设备名称和次数
                                //entity.YLSBCountMaxName = chargeItemname;
                                //entity.YLSBCountMax = chargeItemMax;
                                //entity.YLSBJCMaxFee = chargeItemFee;




                                //#endregion


                                #endregion


                                #region 得要药品的详细信息

                                //药品使用情况(抗生素)
                                DataSet dsTemp = new DataSet();
                                dsTemp = dal.GetC3_2RecordYPKSInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), codedetails[Constants.CODEDETAIL_KSS].Name);

                                int kssls = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : dsTemp.Tables[0].Rows.Count;
                                switch (kssls)
                                {
                                    case 0:
                                        entity.KSSLS = "D";
                                        entity.KSSName = "";
                                        break;
                                    case 1:
                                        entity.KSSLS = "A";
                                        entity.KSSName = dsTemp.Tables[0].Rows[0]["TRADENAME"].ToString();
                                        entity.KSSYPFee = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"].ToString());

                                        break;
                                    case 2:
                                        entity.KSSLS = "B";
                                        entity.KSSName = dsTemp.Tables[0].Rows[0]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[1]["TRADENAME"].ToString();
                                        entity.KSSYPFee = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[1]["MONEY"]);

                                        break;
                                    case 3:
                                        entity.KSSLS = "C";
                                        entity.KSSName = dsTemp.Tables[0].Rows[0]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[1]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[2]["TRADENAME"].ToString();
                                        entity.KSSYPFee = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[1]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[2]["MONEY"]);

                                        break;
                                    default:
                                        entity.KSSLS = "C";
                                        entity.KSSName = dsTemp.Tables[0].Rows[0]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[1]["TRADENAME"].ToString() + "/" + dsTemp.Tables[0].Rows[2]["TRADENAME"].ToString();
                                        entity.KSSYPFee = Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[1]["MONEY"]) + Convert.ToDecimal(dsTemp.Tables[0].Rows[2]["MONEY"]);

                                        break;
                                }

                                // entity.KSSLS = switch kssls ;
                                // entity.KSSName = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count > 1) ? "" : dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"].ToString();

                                //药品使用情况(激素类)
                                dsTemp = dal.GetC3_2RecordYPJSInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), codedetails[Constants.CODEDETAIL_JS].Name);

                                entity.JSCount = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count < 1) ? "" : dsTemp.Tables[0].Rows.Count.ToString();
                                if (dsTemp != null && dsTemp.Tables[0].Rows.Count > 0)
                                {
                                    foreach (DataRow item in dsTemp.Tables[0].Rows)
                                    {
                                        entity.JSName += item["TRADENAME"].ToString() + "/";
                                        entity.JSYPFee += Convert.ToDecimal(item["MONEY"]);

                                    }
                                    entity.JSName = entity.JSName.Remove(entity.JSName.Length - 2);
                                }
                                else
                                {
                                    entity.JSName = "--";
                                    entity.JSYPFee = 0;
                                }



                                //药品使用情况(营养类)
                                dsTemp = dal.GetC3_2RecordYPYYInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), codedetails[Constants.CODEDETAIL_YY].Code);

                                entity.YYCount = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count < 1) ? "" : dsTemp.Tables[0].Rows.Count.ToString();
                                if (dsTemp != null && dsTemp.Tables[0].Rows.Count > 0)
                                {
                                    foreach (DataRow item in dsTemp.Tables[0].Rows)
                                    {
                                        entity.YYName += item["TRADENAME"].ToString() + "/";
                                        entity.YYYPFee += Convert.ToDecimal(item["MONEY"]);
                                    }
                                    entity.YYName = entity.YYName.Remove(entity.YYName.Length - 2);
                                }
                                else
                                {
                                    entity.YYName = "--";
                                    entity.YYYPFee = 0;
                                }



                                dsTemp = dal.GetC3_2RecordYPInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()));
                                entity.YPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]); ;
                              
                                //dsTemp = dal.GetC3_2RecordOtherYPInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()));
                                //entity.OtherYPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]); ;

                                entity.OtherYPFee = 0;

                                #endregion

                                #region 辅助检查费用

                                //辅助检查费用
                                //entity.OtherFZJCFee = 0;
                                //entity.YLSBJCFee = 0;
                                //entity.YLSBJCCount = 0;

                                entity.YLSBJCYXCount = 0;
                                //entity.YLSBCountMaxName = "";
                                //entity.YLSBCountMax = 0;
                                entity.YLSBJCMaxYXCount = 0;
                                //entity.YLSBJCMaxFee = 0;


                                dsTemp = dal.GetC3_2RecordZLBaseInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), Constants.CHARGESUMMARY_ZL);

                                entity.OtherFZJCFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["MONEY"]);


                                dsTemp = dal.GetC3_2RecordZLInfo(row["INHOSID"].ToString(), Convert.ToInt32(row["CURRENTINHOSMARK"].ToString()), itemdetails[0].Name);

                                //entity.JSCount = (dsTemp == null) ? "" : (dsTemp.Tables[0].Rows.Count > 1) ? "" : dsTemp.Tables[0].Rows.Count.ToString();
                                string chargeItemname = "";
                                int chargeItemMax = 0;
                                decimal chargeItemFee = 0;
                                if (dsTemp != null && dsTemp.Tables[0].Rows.Count > 0)
                                {
                                    foreach (DataRow item in dsTemp.Tables[0].Rows)
                                    {
                                        int count = Convert.ToInt32(item["CHARGECOUNT"]);
                                        if (count > chargeItemMax)
                                        {
                                            chargeItemMax = count;
                                            chargeItemname = item["CHARGEITEM"].ToString();
                                            chargeItemFee = Convert.ToDecimal(item["MONEY"]);
                                        }
                                        entity.YLSBJCFee += Convert.ToDecimal(item["MONEY"]);
                                        entity.YLSBJCCount += Convert.ToInt32(item["CHARGECOUNT"]);


                                    }

                                }
                                else
                                {
                                    entity.YLSBJCFee = 0;
                                    entity.YLSBJCCount = 0;
                                }
                                //设置使用次数最大的设备名称和次数
                                entity.YLSBCountMaxName = chargeItemname;
                                entity.YLSBCountMax = chargeItemMax;
                                entity.YLSBJCMaxFee = chargeItemFee;




                                #endregion

                                listC3_2.Add(entity);

                            }
                            else
                            {
                                C3_2Entity ent = new C3_2Entity();
                                ent.BZType = bzentity.Index;
                                ent.BZName = bzentity.BZName;
                                //基本信息
                                ent.HYZID = "--";
                                ent.DiagnoseID = "--";
                                ent.PatientName = "--";
                                ent.Sex = "--";
                                ent.Age = 0;
                                ent.TotalFee = 0;
                                ent.ZFFee = 0;
                                ent.BedCount = 0;


                                ent.KSSLS = "--";
                                ent.KSSName = "--";
                                ent.KSSYPFee = 0;
                                ent.JSName = "--";
                                ent.JSYPFee = 0;
                                ent.JSCount = "0";
                                ent.YYName = "--";
                                ent.YYYPFee = 0;
                                ent.YYCount = "0";
                                ent.YPFee = 0;
                                ent.OtherYPFee = 0;
                                ent.OtherFZJCFee = 0;
                                ent.YLSBJCFee = 0;
                                ent.YLSBJCCount = 0;

                                ent.YLSBJCYXCount = 0;
                                ent.YLSBCountMaxName = "";
                                ent.YLSBCountMax = 0;
                                ent.YLSBJCMaxYXCount = 0;
                                ent.YLSBJCMaxFee = 0;


                                listC3_2.Add(ent);
                            }


                        }


                    }

                    temp++;
                }
            }
            catch (Exception ex)
            {
                log.Error("C32BLL-->" + ex.Message);
            }
            return listC3_2;

        }
    }
}
